package repository

import (
	"fmt"
	"os"

	"gitverse.ru/pahtll/checats/src/internal/models"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

var (
	DB_HOST     = os.Getenv("DB_HOST")
	DB_PORT     = os.Getenv("DB_PORT")
	DB_DATABASE = os.Getenv("DB_DATABASE")
	DB_USERNAME = os.Getenv("DB_USERNAME")
	DB_PASSWORD = os.Getenv("DB_PASSWORD")
)

type Repository struct {
	db *gorm.DB
}

func NewRepository() *Repository {
	db, err := gorm.Open(
		postgres.Open(
			fmt.Sprintf("host=%v;User Id=%v;Password=%v;Database=%v;Port=%v",
				DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_PORT),
		),
		&gorm.Config{},
	)
	if err != nil {
		panic(err)
	}

	db.AutoMigrate(
		&models.User{},
		&models.Post{},
		&models.Commentary{},
	)

	return &Repository{
		db: db,
	}
}
